package com.lfg.leetcode.leetcode0;

import java.util.Stack;

public class No71simplifyPath {

    public static void main(String[] args) {
//        String ret = simplifyPath("/a/b/c/d/f");
        String ret = simplifyPath("/..");
        System.out.println("[" + ret + "]");
    }

    public static String simplifyPath(String path) {
        if (null == path || "".equals(path)) {
            return null;
        }
        String[] paths = path.split("/");
        Stack<String> stack = new Stack<>();
        for (String str : paths) {
            if ("".equals(str)) {
            } else if (".".equals(str)) {
            } else if ("..".equals(str)) {
                if (!stack.isEmpty()) {
                    stack.pop();
                }
            } else {
                stack.push(str);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (String str : stack) {
            sb.append("/").append(str);
        }
        if (sb.length() == 0) {
            return "/";
        }
        return sb.toString();
    }
}

